package net.ojbk.timeSpace.test.web.handllers.impl;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import net.ojbk.timeSpace.test.beans.APIResult;
import net.ojbk.timeSpace.test.beans.Msg;
import net.ojbk.timeSpace.test.beans.TimeAxis;
import net.ojbk.timeSpace.test.beans.TimeBlade;
import net.ojbk.timeSpace.test.common.api.msgApi.APIMsgCode;
import net.ojbk.timeSpace.test.common.api.timeBladApi.APITimeBladCode;
import net.ojbk.timeSpace.test.service.IAxisService;
import net.ojbk.timeSpace.test.service.IBladeService;
import net.ojbk.timeSpace.test.web.handllers.IBladeController;

@Controller
public class BladeController implements IBladeController {

	@Autowired
	@Qualifier("bladeService")
	private IBladeService bladeService;

	@Autowired
	@Qualifier("axisService")
	private IAxisService axisService;

	@Override
	@RequestMapping("/deleteBlade.do")
	@ResponseBody    // 暂时OK
	public Object doDeleteBlade(int timeId, HttpSession session) {  // 缺陷： 未删除对应图片与评论
		// TODO Auto-generated method stub

		@SuppressWarnings("unchecked")
		List<TimeBlade> timeBladeList = (List<TimeBlade>) session.getAttribute("timeBladeList");
		
		int axisId = bladeService.getAxisId(timeId, timeBladeList);
		bladeService.deleteBlade(timeId);
		return APIResult.createSuccess(new APITimeBladCode(), axisId);
	}

	@Override
	@RequestMapping("/sendMsg.do")        //OK
	@ResponseBody
	public Object doSendMsg(int timeId, String msgPw, String MsgName, String content, HttpSession session) {

		// 检查密码
//		@SuppressWarnings("unchecked")
//		List<TimeBlade> timeBladeList = (List<TimeBlade>) session.getAttribute("timeBladeList");  
//		int axisId = bladeService.getAxisId(timeId, timeBladeList);
		int axisId = 1000;
		TimeAxis timeAxis = new TimeAxis();
		timeAxis.setAxisId(axisId);
		if (axisService.checkMsgPw(msgPw, timeAxis)) {
			if (MsgName == null) {
				Msg msg = new Msg();
				msg.setTimeBladeId(timeId);
				msg.setMsgContent(content);
				msg.setTimeBladeId(timeId);
				msg.setMsgName("匿名");
				bladeService.pushMsg(msg);
			} else {
				Msg msg = new Msg();
				msg.setTimeBladeId(timeId);
				msg.setMsgContent(content);
				msg.setTimeBladeId(timeId);
				msg.setMsgName(MsgName);
				bladeService.pushMsg(msg);
			}
			return APIResult.createSuccessMessage(new APIMsgCode(), "发表评论成功");
		}
		return APIResult.createFail(new APIMsgCode(), "发表评论失败");
	}

	@Override
	@RequestMapping("/getAllMsg.do")  //OK
	@ResponseBody
	public Object doGetAllMsg(int timeId) {
		// TODO Auto-generated method stub
		List<Msg> msgList = bladeService.getAllMsgByBladeId(timeId);
		for (Msg t : msgList) {
			System.out.println(t.getMsgTime());
		}
		return APIResult.createSuccess(new APIMsgCode(), msgList);
	}

	@Override
	@RequestMapping("/getTimeInfo.do")  //OK
	@ResponseBody
	public Object doGetTimeInfo(int timeId) {
		// TODO Auto-generated method stub
		TimeBlade timeBlade = bladeService.getBladeInfo(timeId);
		if(timeBlade!=null)
			return APIResult.createSuccess(new APITimeBladCode(), timeBlade);
		return APIResult.createFail(new APITimeBladCode(), "获取叶片信息失败");
	}

}
